/**
 * 
 */
package cn.springsoter.core.tool.jackson;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

/**
 * Jackson null值特殊化处理：
 * 
 * 1、处理数组类型的null值 -> []
 * 2、处理字符串类型的null值 -> ""
 * 3、处理Integer类型的null值 -> 0
 * 4、处理Long类型的null值 -> ""
 * 5、处理布尔类型的null值 -> false
 * 
 * 除上面5种之外的其他类型/对象，不做处理，保持null值
 * 
 * @author kevin
 *
 */
public class ApiNullValueJacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {

	/**
     * 处理数组类型的null值 -> []
     */
    public class NullArrayJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
        	jsonGenerator.writeStartArray();
        	jsonGenerator.writeEndArray();
        }
    }
 
    /**
     * 处理字符串类型的null值 -> ""
     */
    public class NullStringJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        	jsonGenerator.writeString(StringUtils.EMPTY);
        }
    }
 
    /**
     * 处理Integer类型的null值 -> 0
     */
    public class NullIntegerJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        	jsonGenerator.writeNumber(0);
        }
    }
    
    /**
     * 处理Long类型的null值 -> ""
     */
    public class NullLongJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        	jsonGenerator.writeString(StringUtils.EMPTY);
        }
    }
 
    /**
     * 处理布尔类型的null值 -> false
     */
    public class NullBooleanJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        	jsonGenerator.writeBoolean(false);
        }
    }
    
    
    public class MyBeanSerializerModifier extends BeanSerializerModifier {
        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            //循环所有的beanPropertyWriter
            for (Object beanProperty : beanProperties) {
                BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;
                //判断字段的类型，如果是array，list，set则注册nullSerializer
                if (isArrayType(writer)) {
                    //给writer注册一个自己的nullSerializer
                    writer.assignNullSerializer(new NullArrayJsonSerializer());
                } else if (isIntegerType(writer)) {
                    writer.assignNullSerializer(new NullIntegerJsonSerializer());
                } else if (isBooleanType(writer)) {
                    writer.assignNullSerializer(new NullBooleanJsonSerializer());
                } else if (isStringType(writer)) {
                    writer.assignNullSerializer(new NullStringJsonSerializer());
                } else if (isLongType(writer)) {
                    writer.assignNullSerializer(new NullLongJsonSerializer());
                }
            }
            return beanProperties;
        }
 
        /**
         * 是否是数组
         */
        private boolean isArrayType(BeanPropertyWriter writer) {
            Class<?> clazz = writer.getType().getRawClass();
            return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
        }
 
        /**
         * 是否是string
         */
        private boolean isStringType(BeanPropertyWriter writer) {
            Class<?> clazz = writer.getType().getRawClass();
            return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
        }
 
 
        /**
         * 是否是Integer
         */
        private boolean isIntegerType(BeanPropertyWriter writer) {
            Class<?> clazz = writer.getType().getRawClass();
            return Integer.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz);
        }
        
        /**
         * 是否是Long
         */
        private boolean isLongType(BeanPropertyWriter writer) {
            Class<?> clazz = writer.getType().getRawClass();
            return Long.class.isAssignableFrom(clazz);
        }
 
        /**
         * 是否是boolean
         */
        private boolean isBooleanType(BeanPropertyWriter writer) {
            Class<?> clazz = writer.getType().getRawClass();
            return clazz.equals(Boolean.class);
        }
 
    }
}
